package com.smalluz.ad.modules.oe.api.impl;

import java.util.Map;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.smalluz.ad.modules.common.config.MediaConfigStorage;
import com.smalluz.ad.modules.common.exception.ApiException;
import com.smalluz.ad.modules.common.result.BaseResult;
import com.smalluz.ad.modules.common.tools.OkHttpUtils;
import com.smalluz.ad.modules.common.util.MediaConfigStorageHolder;
import com.smalluz.ad.modules.oe.api.OeAccountService;
import com.smalluz.ad.modules.oe.api.OeClueService;
import com.smalluz.ad.modules.oe.api.OeReportService;
import com.smalluz.ad.modules.oe.api.OeService;
import com.smalluz.ad.modules.oe.enums.ErrorCodeEnum;


import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class BaseOeServiceImpl implements OeService {

    @Getter
    @Setter
    private OeClueService oeClueService = new OeClueServiceImpl(this);
    
    @Getter
    @Setter
    private OeAccountService oeAccountService = new OeAccountServiceImpl(this);

    @Getter
    @Setter
    private OeReportService oeReportService = new OeReportServiceImpl(this);

    @Getter
    @Setter
    private OkHttpUtils okHttpUtils = new OkHttpUtils();

    @Getter
    @Setter
    private Map<String, MediaConfigStorage> configStorageMap;

    @Override
    public OeService switchoverTo(String advertiserId) {
        if (this.configStorageMap.containsKey(advertiserId)) {
            MediaConfigStorageHolder.set(advertiserId);
            return this;
        }
        
        log.info("~~~~切换失败:{}",advertiserId);
        throw new ApiException("切换失败...");
    }

    @Override
    public MediaConfigStorage getMediaConfigStorage() {
        if (this.configStorageMap.size() == 1) {
            //如果只有一个账号的情况下直接返回属性
            return this.configStorageMap.values().iterator().next();
        }

        return this.configStorageMap.get(MediaConfigStorageHolder.get());
    }

    @Override
    public void setMultiConfigStorages(Map<String, MediaConfigStorage> configStorages) {
        this.setMultiConfigStorages(configStorages, configStorages.keySet().iterator().next());
    }

    @Override
    public void setMultiConfigStorages(Map<String, MediaConfigStorage> configStorages, String defaultAdId) {
        this.configStorageMap = Maps.newHashMap(configStorages);
        MediaConfigStorageHolder.set(defaultAdId);
    }

    @Override
    public void setConfigStorage(MediaConfigStorage configProvider) {
        final String defaultAdId = configProvider.getAdvertiserId();
        if (defaultAdId == null) {
            throw new ApiException("advertiserId不能设置为null");
        }

        this.setMultiConfigStorages(ImmutableMap.of(defaultAdId, configProvider), defaultAdId);
    }

    @Override
    public void addConfigStorage(String advertiserId, MediaConfigStorage configStorages) {
        synchronized (this) {
            if (this.configStorageMap == null) {
                this.setConfigStorage(configStorages);
            } else {
                MediaConfigStorageHolder.set(advertiserId);
                this.configStorageMap.put(advertiserId, configStorages);
            }
        }
    }
    
   
    @Override
    public Boolean isCancelAuthorization(BaseResult baseResult) {
        //如果token错误或者refreshToken错误那么说明该应用已经取消授权了，这么判断可能过于武断后续再看有没有更好的方案
        if(ErrorCodeEnum.AuthEnum.INVALID_ACCESS_TOKEN.getCode().equals(baseResult.getCode())) {
            return true;
        }else if(ErrorCodeEnum.AuthEnum.INVALID_REFRESH_TOKEN.getCode().equals(baseResult.getCode())) {
            return true;
        }else if(ErrorCodeEnum.CommonEnum.PERMISSION_ERROR.getCode().equals(baseResult.getCode())) {
            return true;
        }

        return false;
    }
    
    protected String post(String accessToken,String path, String jsonStr) {
        return this.okHttpUtils.post(path, jsonStr);
    }
    
    protected String get(String accessToken, String path, String jsonStr) {
        return this.okHttpUtils.get(accessToken, path, jsonStr);
    }


}
